package com.star.spring.reactor.flux;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 调度器demo
 * @author: star
 * @create: 2025-03-17 13:51
 **/

public class ScheduleDemo {

    private static final Logger log = LoggerFactory.getLogger(ScheduleDemo.class);

    private static Mono<String> captureImage(String streamUrl) {
        return Mono.fromCallable(() -> {
            try {
                log.debug("抓取图片，streamUrl: {}", streamUrl);
                sayHello(streamUrl);

                Thread.sleep(5000);
            } catch (Exception e) {
                log.error("Failed to capture image from stream: {}", streamUrl, e);
                return null;
            }
            return "捕捉到了"+ streamUrl;
        }).publishOn(Schedulers.boundedElastic());
    }

    private static void sayHello(String name) {

        System.out.println("hello " + name);
    }

    public static void main(String[] args) throws IOException {

        List<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        strings.add("3");

        Flux.fromIterable(strings)

                .flatMap(ScheduleDemo::captureImage)
                .collectList()
                .subscribe(item -> {
                    log.info("item: {}", item);
                });
        // 等待系统有输入在结束
        System.in.read();

    }
}
